/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> nums;
    queue<TreeNode*> q;
    vector<vector<int>> levelOrder(TreeNode* root) {
        if(nums.size()) nums.clear();
        if(root) q.push(root);
        int i=0,j=0;
        int count=0;
        while(!q.empty()){
            int cursize=q.size();
            vector<int> vt;
            for(int i=0;i<cursize;i++){
                TreeNode* newNode=q.front();
                vt.push_back(newNode->val);
                if(newNode->left) q.push(newNode->left);
                if(newNode->right) q.push(newNode->right);
                q.pop();
            }
            //如果层数为奇数，则逆置临时数组vt;vt;
            if(count%2==1){
                reverse(vt.begin(),vt.end());
            }
            nums.push_back(vt);
            count++;
        }
        return nums;
    }
};